import Datetime from "vux/src/components/datetime/index.vue";
import dateFormat from "vux/src/tools/date/format.js";

import template from "./recordTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  components: {
    Datetime,
  },
  data() {
    return {
      pullUpLoad: true,
      pullDownRefresh: true,
      pullTxt: "松开立即加载更多",
      page: 1,
      historyList: [],
      thisYear: dateFormat(new Date(), "YYYY"),
      loadingHttp: false,

      redpacketCount: [],
    };
  },
  created() {
    this._getRedpacketCount();
  },
  mounted() {},
  methods: {
    removePic() {
      this.redpacketCount.self_pic = "";
    },
    removeImg(index) {
      this.historyList[index].redpacket_pic = "";
    },
    _getRedpacketCount() {
      //历史统计第一页数据
      if (!JSON.parse(localStorage.getItem("isUser"))) {
        this.$vux.toast.show({
          text: "游客暂无权限，请先注册会员",
        });
        return;
      }
      var params = {
        token: localStorage.getItem("token"),
        year: this.thisYear,
      };
      this.$vux.loading.show();
      this.$http
        .post(this.urlRequest + "?m=api&c=redpacket&a=redpacketCount", params)
        .then((res) => {
          this.$vux.loading.hide();
          if (res.status == 0) {
            this.loadingHttp = true;
            this.redpacketCount = res;
            this.historyList = res.history_list;
            console.log(res);
          } else {
            if (res.ret_msg && res.ret_msg != "") {
              this.$vux.toast.show({
                text: res.ret_msg,
              });
            }
          }
        })
        .catch((error) => {
          this.$vux.loading.hide();
          this.$vux.toast.show({
            text: "数据请求超时",
          });
        });
    },
    _getInstantlyBetting(flag) {
      //历史记录 加载更多数据
      var params = {
        token: localStorage.getItem("token"),
        year: this.thisYear,
        page: this.page,
      };
      if (flag) {
        this.$vux.loading.show();
      }
      this.$http
        .post(
          this.urlRequest + "?m=api&c=redpacket&a=selfRedpacketHistory",
          params
        )
        .then((res) => {
          if (flag) {
            this.$vux.loading.hide();
          }
          if (res.status == 0) {
            if (this.page == 1) {
              this.historyList = res.history_list;
            } else {
              this.historyList = this.historyList.concat(res.history_list);
            }
            if (res.history_list.length < 10) {
              this.pullUpLoadFalg = true;
            }
          } else {
            if (res.ret_msg && res.ret_msg != "") {
              this.$vux.toast.show({
                text: res.ret_msg,
              });
            }
          }
        })
        .catch((error) => {
          this.$refs.scroll.forceUpdate(false);
          this.pullUpLoad = false;
          this.$vux.loading.hide();
          this.$vux.toast.show({
            text: "数据请求超时",
          });
        });
    },
    pullingDown() {
      this.page = 1;
      this._getInstantlyBetting(0);
    },
    pullingUp() {
      if (!this.historyList) {
        return;
      }
      if (this.pullUpLoadFalg) {
        this.$refs.scroll.forceUpdate(false);
        return false;
      }
      this.page += 1;
      this._getInstantlyBetting(0);
    },
  },
  watch: {
    thisYear() {
      this.page = 1;
      this._getRedpacketCount();
      this.pullUpLoadFalg = false;
      this.$refs.scroll.scrollTo(0, 0);
    },
  },
};

// WEBPACK FOOTER //
// ./src/components/redPacket/record.vue
